
//
//  AddPatViewController.m
//  LiverClient
//
//  Created by dongshangtong on 16/5/12.
//  Copyright © 2016年 朱亚杰. All rights reserved.
//

#import "AddPatViewController.h"
#import "PayOrderViewController.h"
#import "YCSellRightModel.h"


@interface AddPatViewController () <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *patName;
@property (weak, nonatomic) IBOutlet UILabel *patSex;
@property (weak, nonatomic) IBOutlet UILabel *patDate;
- (IBAction)selectClick:(id)sender;
@property (weak, nonatomic) IBOutlet UITextField *IdCard;
@property (weak, nonatomic) IBOutlet UITextField *mobile;
@property (weak, nonatomic) IBOutlet UIButton *sureBtn;
@property (weak, nonatomic) IBOutlet UIButton *keyButton;



@property (nonatomic, weak) UIPickerView *pickerView;
@property (nonatomic, strong) UIPickerView *popViewSex;

@property (nonatomic, strong) UIView *popViewDate;
@property (nonatomic, strong) UIView * popView;

@property(nonatomic,weak)NSDate* dateAge;



@property (nonatomic, strong) UIButton * bgBtn;

@end

@implementation AddPatViewController

#pragma mark - Life Cycle


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"添加保障人信息";
    
    self.patName.returnKeyType =UIReturnKeyDone;
    self.patName.delegate = self;
    self.IdCard.returnKeyType =UIReturnKeyDone;
    self.IdCard.delegate = self;
    self.mobile.returnKeyType =UIReturnKeyDone;
    self.mobile.delegate = self;

    
    
    self.sureBtn.layer.cornerRadius = 4;
    
    self.sureBtn.clipsToBounds = YES;
    self.sureBtn.backgroundColor = [UIColor colorWithRGBValue:0x0092F0];
    

    
    
    [self.bgBtn addSubview:self.popView];
    [self.bgBtn addSubview:self.popViewDate];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endEditing:) name:UITextFieldTextDidChangeNotification object:nil];
    
//        if (!self.personModel.personBirth)
//        {
//            if ([NSString hdf_isValidPersonID:self.personModel.personIDCard])
//            {
//                [self getDateStr:self.personModel.personIDCard];
//
//            }
//
//        }
        
//        if (!self.personModel.personSex)
//        {
//            self.patSex.text = [YCtool validateIdentifierCardSex:self.personModel.personIDCard];
//
//        }
        
    
}

- (void)getDateStr:(NSString *)idCardStr
{
    NSString * yearStr = [self.IdCard.text substringWithRange:NSMakeRange(6, 4)];
    NSString * monthStr = [self.IdCard.text substringWithRange:NSMakeRange(10, 2)];
    NSString * dayStr = [self.IdCard.text substringWithRange:NSMakeRange(12, 2)];
    self.patDate.text = [NSString stringWithFormat:@"%@-%@-%@",yearStr,monthStr,dayStr];


}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - Notificaiton

- (void)endEditing:(NSNotification *)noti
{
   

    if (self.IdCard.text.length == 18)
    {
        
        self.patSex.text = [NSString yc_validateIdentifierCardSex:self.IdCard.text];
        
        NSString * yearStr = [self.IdCard.text substringWithRange:NSMakeRange(6, 4)];
        NSString * monthStr = [self.IdCard.text substringWithRange:NSMakeRange(10, 2)];
        NSString * dayStr = [self.IdCard.text substringWithRange:NSMakeRange(12, 2)];
        self.patDate.text = [NSString stringWithFormat:@"%@-%@-%@",yearStr,monthStr,dayStr];
        
    }
    
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    
    [self.view endEditing:YES];
}



-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    
    [self.view endEditing:YES];
    
    return YES;
}


#pragma mark -单击不同的按钮

- (IBAction)selectClick:(UIButton *)sender {
    
    
    if (self.patName.text.length==0) {
        
        [UIAlertController alertTitleTip:@"名字不能为空"];
        return;
    }

    
    if ([NSString yc_inputShouldNumber:self.patName.text]) {
        
        [UIAlertController alertTitleTip:@"名字格式不对"];
        return;
    }

    
    
    if (![NSString yc_isValidPersonID:self.IdCard.text]) {
        
        [UIAlertController alertTitleTip:@"身份号格式不对"];
        return;
    }
    

    
    if (![NSString yc_isValidPhone:self.mobile.text]) {
        
        [UIAlertController alertTitleTip:@"手机号格式不对"];
        return;
     }
    
    
    
    
        [self addpatdataSoure];
    
}


/**
 添加病人
 */
-(void)addpatdataSoure
{
    
       NSString *sex =@"0";
    
    if ([self.patSex.text isEqualToString:@"女"]) {
        sex = @"1";
    }
    
    NSDictionary * params = @{
                              @"person_name":self.patName.text,
                              @"person_mobile":self.mobile.text,
                              @"person_idcard":self.IdCard.text,
                              @"member_mobile":[YCUserTool sharedTools].linkMobile,
                              @"person_age":[NSString yc_validateIdentifierCardAge:self.IdCard.text],
                              @"person_sex":sex,
                              @"yw_id":yw_id,
                              @"sale_userid":[YCUserTool sharedTools].userID,
                              @"sale_mobile":[YCUserTool sharedTools].linkMobile,
                              @"sale_type":self.model.SALE_TYPE,
                              @"hz_money":self.model.CARD_PRICE
         
                              };
    
    NSString *url =bxbuyokUrl;
    [self presentLoadingTips:nil];
    [DownloadData post: url params:params success:^(id json) {
        [self dismissTips];
        
        NSString *orderid = json[@"order_id"];
        if (orderid) {
            PayOrderViewController  *PayVC  = [[PayOrderViewController alloc]init];
            YCReadingModel *model = [[YCReadingModel alloc]init];
            model.orderId = orderid;
            model.payMoney = self.model.CARD_PRICE;
            model.hosName = self.model.CARD_NAME;
            PayVC.readingModel = model;
            [self.navigationController pushViewController:PayVC animated:YES];
        }else{
         
            [self presentFailureTips:@"提交信息失败!"];
            
        }
        
    } failure:^(NSError *error) {
        
        [self dismissTips];
        
    }];
    
    
    
    
    
    
}



-(NSString*)getPersonAge:(NSString*)personCardAge
{

    NSString* personAge = personCardAge ;
    
    if ([personCardAge containsString:@"岁"]) {
        
        NSRange rang = [personCardAge rangeOfString:@"岁"];
        
        NSInteger index = rang.location;
        
      personAge = [personCardAge substringToIndex:index];
        
    }
    
    return personAge;
    
}

@end























